//
//  SystemMessageViewController.m
//  MiaoTu
//
//  Created by dllo on 16/9/12.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "SysMsgViewController.h"
#import "ZWYNetTool.h"
#import "Message.h"
#import "MessageItem.h"
#import "Content.h"
#import "SysMsgTableCell.h"
#import "UIView+Frame.h"

#define kSysMsgTableCell @"SysMsgTableCellIdentifier"


@interface SysMsgViewController ()
@property (nonatomic, strong) Message *messageModel;
@property (nonatomic, strong) NSArray<MessageItem *> *messageArray;
@end

@implementation SysMsgViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
//    [self.tableView registerClass:[SysMsgTableCell class] forCellReuseIdentifier:kSysMsgTableCell];
    
    [self createData];
}

- (void)createData {
    NSDictionary *headerDic = @{@"User-Agent":@"Dalvik/1.6.0 (Linux; U; Android 4.3; R6007 Build/JLS36C) Paros/3.2.13", @"Host":@"api.miaotu.net", @"Connection":@"Keep-Alive"};
    [ZWYNetTool GET:@"http://api.miaotu.net/v2/user/msg/system?token=abf776af-6a90-11e6-8ccf-00163e002e59&type=system&value=&page=1 &num=100000" andBody:nil andHeader:headerDic andResponse:ZWYJSON andSuccessBlock:^(id result) {
        self.messageModel = [Message modelWithDic:result];
        self.messageArray = _messageModel.Items;
        [self.tableView reloadData];
    } andFailureBlock:^(NSError *error) {
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _messageArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SysMsgTableCell *cell = [tableView dequeueReusableCellWithIdentifier:kSysMsgTableCell];
    if (cell == nil) {
        cell = [[SysMsgTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kSysMsgTableCell];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.messageItem = _messageArray[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    SysMsgTableCell *cell = (SysMsgTableCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
    
    CGRect rect = [cell.rootLayout estimateLayoutRect:CGSizeMake(self.tableView.dk_width, 0)];
    
    return rect.size.height;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
